package cn.mw.controller;

import cn.mw.domain.User;
import cn.mw.dto.OrdDto;
import cn.mw.service.IUserService;
import cn.mw.util.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 增加
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        try {
            userService.add(user);
        } catch (Exception e) {
            return new AjaxResult().setResultObj("操作失败");
        }
        return new AjaxResult();
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userService.delete(id);
        } catch (Exception e) {
            return new AjaxResult().setResultObj("操作失败");
        }
        return new AjaxResult();
    }

    /**
     * 修改
     */
    @PostMapping("/updete")
    public AjaxResult updete(@RequestBody User user) {
        try {
            userService.update(user);
        } catch (Exception e) {
            return new AjaxResult().setResultObj("操作失败");
        }
        return new AjaxResult();
    }

    /**
     *查询单个
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        try {
            User user = userService.findOne(id);
            return new AjaxResult().setResultObj(user);
        } catch (Exception e) {
            return new AjaxResult().setResultObj("操作失败");
        }
    }
    /**
     * 查询多个
     */
    @GetMapping("/findAll")
    public AjaxResult findAll(){
        try {
            List<User> users = userService.findAll();
            return new AjaxResult().setResultObj(users);
        } catch (Exception e) {
            return new AjaxResult().setResultObj("操作失败");
        }
    }
    @GetMapping("/getOne/{id}")
    public User getOne(@PathVariable("id") Long id) {
        User user = userService.getOne(id);
        return user;
    }

}
